/* share.c - share */

#include <xinu.h>

/*------------------------------------------------------------------------
 *  share  -  Take addr of a previously allocated heap memory as an
		argument and specify the block is shared
 *------------------------------------------------------------------------
 */
syscall	share(
		char	*addr,	/* Address of an allocated heap block	*/
		pid32	pid	/* Process to share the memory with	*/
	)
{
	intmask	mask;		/* saved interrupt mask		*/
	struct 	heapblk	*block;	/* pointer to the allocated heap block	*/
	char 	*realaddr;
	struct	procent *prptr;	/* ptr to process' table entry	*/
	int32 i = 0;

	mask = disable();

	realaddr = (char *)((uint32) addr - 16);
	block = (struct heapblk *)realaddr;
	block->sharec ++;
	
	prptr = &proctab[pid];
	while(prptr->shheap[i] != NULL) {
		i++;
	}
	prptr->shheap[i] = realaddr;
	
	restore(mask);
	return OK;
}
